package com.elvt.server.service.impl;
import java.time.LocalDateTime;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elvt.server.mapper.EmployeeMapper;
import com.elvt.server.mapper.MailLogMapper;
import com.elvt.server.pojo.Employee;
import com.elvt.server.pojo.MailConstants;
import com.elvt.server.pojo.MailLog;
import com.elvt.server.service.IEmployeeService;
import com.elvt.server.utils.RespPageUtil;
import com.elvt.server.utils.RespSult;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 作者：elvt
 * @since 2022-05-21
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private MailLogMapper mailLogMapper;
    /**
     * 分页
     * @param currentPage 分页的页数
     * @param size 分页的条数
     * @param employee 条件
     * @param beginDataSource 开始时间
     * @return
     */
    @Override
    public RespPageUtil getEmployeeByPage(Integer currentPage, Integer size, Employee employee, LocalDate[] beginDataSource) {
        //开启分页
       Page<Employee> page=new Page<>(currentPage,size);
        IPage<Employee> iPage = employeeMapper.getEmployeeByPage(page, employee, beginDataSource);
        RespPageUtil respPageUtil=new RespPageUtil(iPage.getTotal(),iPage.getRecords());

        return respPageUtil;
    }

    @Override
    public RespSult maxWorkID() {
        List<Map<String, Object>> maps = employeeMapper.selectMaps(new QueryWrapper<Employee>().select("max(workID)"));


        return RespSult.success(null,String.format("%08d", Integer.parseInt(maps.get(0).get("max(workID)").toString())+1));
    }
    /**
     * 添加员工
     * @param employee
     * @return
     */
    @Override
    public RespSult addEmp(Employee employee) {
        //合同开始时间
        LocalDate beginContract = employee.getBeginContract();
        //合同结束时间
        LocalDate endContract = employee.getEndContract();
        //两个日期相差多少天
        long days = beginContract.until(endContract, ChronoUnit.DAYS);
        //格式化保留两位小数
        DecimalFormat decimalFormat=new DecimalFormat("##.00");
        //不判断闰年的
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(days/365.00)));
        if (0<employeeMapper.insert(employee)){
            //拿到用户对象
            Employee emp = employeeMapper.getAllEmployeeORGgetById(employee.getId()).get(0);
            //数据库记录发送的消息
            String msgId = UUID.randomUUID().toString();
            MailLog mailLog=new MailLog();
            mailLog.setMsgId(msgId);
            mailLog.setEid(employee.getId());
            mailLog.setStatus(0);
            mailLog.setRouteKey(MailConstants.MAIL_ROUTNG_KEY_NAME);
            mailLog.setExchange(MailConstants.MAIL_EXCHANGE_NAME);
            mailLog.setCount(0);
            mailLog.setTryTime(LocalDateTime.now().plusDays(MailConstants.MSG_TIMEOUT));
            mailLog.setCreateTime(LocalDateTime.now());
            mailLog.setUpdateTime(LocalDateTime.now());
            mailLogMapper.insert(mailLog);


           //绑定路由，发送对象
            rabbitTemplate.convertAndSend(MailConstants.MAIL_EXCHANGE_NAME,MailConstants.MAIL_ROUTNG_KEY_NAME,emp,new CorrelationData(msgId));
            return RespSult.success("添加成功！");
        }
        return RespSult.error("添加失败！");
    }
    /**
     * 查询员工，如果id为空，则查询所有
     * @param id
     * @return
     */
    @Override
    public List<Employee> getAllEmployeeORGgetById(Integer id) {
        return employeeMapper.getAllEmployeeORGgetById(id);
    }
    /**
     * 获取所有员工的工资账套
     * @param currentPage 分页
     * @param size 分页条数
     * @return
     */
    @Override
    public RespPageUtil getEmployeeSalary(Integer currentPage, Integer size) {
        //开启分页
        Page<Employee> page=new Page<>(currentPage,size);
        IPage<Employee> employeeIPage= employeeMapper.getEmployeeSalary(page);
        RespPageUtil pageUtil=new RespPageUtil(employeeIPage.getTotal(),employeeIPage.getRecords());
        return pageUtil;
    }
}
